package com.qiulingcloud.commonsserver.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiulingcloud.commonsserver.entity.vo.ConditionVo;
import com.qiulingcloud.commonsserver.entity.vo.PageVO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 0:15 2020/4/28
 * @Modified By：
 */
public abstract class PageUtil {

    /**
     * 封装返回的page对象
     * @param iPage
     * @return
     */
    public static <T> PageVO<T> getPageVO(IPage<T> iPage) {
        PageVO<T> result = new PageVO<>();
        result.setTotalPages(iPage.getPages());
        result.setTotalRows(iPage.getTotal());
        result.setPageNum(iPage.getCurrent());
        result.setPageSize(iPage.getSize());
        result.setList(iPage.getRecords());
        return result;
    }

    /**
     * 自定义查询条件，这里只封装了常用的，更多用法请参考"https://mp.baomidou.com/guide/wrapper.html#abstractwrapper"
     * 前端传值格式为：
     * <1>var searchParam = [
     *        {column: "COLUMN_NAME",type: "like", value: "tim"},
     *    {column: "COLUMN_AGE",type: "eq", value: "22"},
     *    {column: "COLUMN_DATE",type: "ge", value: "2019-08-16 00:00:00"},
     *    {column: "COLUMN_DATE",type: "le", value: "2019-08-16 23:59:59"}
     * ];
     * <2>condition:JSON.stringify(searchParam)
     * <3>PostMan自测 传值 如：[{\"column\":\"deleted\",\"type\":\"eq\",\"value\":\"1\"},{\"column\":\"sex\",\"type\":\"eq\",\"value\":\"1\"}]
     * @param conditionJson
     * @param isAsc true: desc false asc
     * @param orderFields
     * @return
     */
    public static QueryWrapper parseWhereSql(String conditionJson, boolean isAsc, String[] orderFields) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(conditionJson)) {
            List<ConditionVo> conditionList = JSON.parseArray(conditionJson, ConditionVo.class);
            if (!conditionList.isEmpty()) {
                for (ConditionVo conditionVo : conditionList) {
                    switch (conditionVo.getType()) {
                        //"eq"相当于"=" 例: eq("name", "老王")--->name = '老王'"
                        case "eq":
                            queryWrapper.eq(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"ne"相当于"<>" 例: ne("name", "老王")--->name <> '老王'"
                        case "ne":
                            queryWrapper.ne(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"like"相当于"%值%" like("name", "王")--->name like '%王%'
                        case "like":
                            queryWrapper.like(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"likeLeft"相当于"%值" 例: likeLeft("name", "王")--->name like '%王'
                        case "likeLeft":
                            queryWrapper.likeLeft(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"likeRight"相当于"值%" 例: likeRight("name", "王")--->name like '王%'
                        case "likeRight":
                            queryWrapper.likeRight(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"notlike"相当于"%值%" 例: notLike("name", "王")--->name not like '%王%'
                        case "notlike":
                            queryWrapper.notLike(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"gt"相当于">" 例: gt("age", 18)--->age > 18
                        case "gt":
                            queryWrapper.gt(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"lt"相当于"<" 例: lt("age", 18)--->age < 18
                        case "lt":
                            queryWrapper.lt(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"ge"相当于">=" 例: ge("age", 18)--->age >= 18
                        case "ge":
                            queryWrapper.ge(conditionVo.getColumn(), conditionVo.getValue());
                            break;

                        //"le"相当于"<=" 例: le("age", 18)--->age <= 18
                        case "le":
                            queryWrapper.le(conditionVo.getColumn(), conditionVo.getValue());
                            break;
                    }
                }
            }
        }
        /**
         * 驼峰转下划线
         */
        if(orderFields.length > 0 && !orderFields[0].equals("")) {
            String[] columns = new String[orderFields.length];
            for(int i = 0; i < orderFields.length; i++) {
                columns[i] = HumpToUnderline(orderFields[i]);
            }
            queryWrapper.orderBy(true, isAsc, columns);
        }

        return queryWrapper;
    }

    /**
     * 驼峰转下划线
     * @param para
     * @return
     */
    private static String HumpToUnderline(String para){
        StringBuilder sb = new StringBuilder(para);
        int temp=0;//定位
        if (!para.contains("_")) {
            for(int i=0;i<para.length();i++){
                if(Character.isUpperCase(para.charAt(i))){
                    sb.insert(i+temp, "_");
                    temp+=1;
                }
            }
        }
        return sb.toString().toLowerCase();
    }

}
